﻿using System;
using RCSP.Biz.Models;
using RCSP.DataAccess;
using System.Data;
using RCSP.Common;
using RCSP.Common.Enums;

namespace RCSP.Biz
{
    public interface IMemberRepository
    {
        Member GetMemberByID(string memberId);
        PostalCode GetPostalCodeByZipCode(string zipCode);
        District GetDistrictByStateCode(string stateCode);
        Branch GetBranchByBranchCode(int branchCode);
        Bank GetBankByBankCode(int bankCode);
        int SaveMember(RequestType type, string accountNumber, string kanaName, string name, string genderClassification, DateTime? dateOfBirth, string zip, string address1, string address2, string phoneNumber
            , int? DateOfBirthYear, int? dateOfBirthMonth, int? dateOfBirthDay
            , string areaCode, string billingCode, string billingClassification
            , string paymentMethod, string paymentDivision, int? yearBillingMonth, int? numberOfUnits, int? bankCode
            , int? branchCode, string depositEvent, string account, string accountName, string parentMembershipNumber
            , string workPostalCode, string workAddress2, string workPhoneNumber, string memberClassification
            , int? dMDestination, DateTime? withdrawalDate, DateTime? changeProcessDate, string theReasonForStoppingUse
            , string hospital, string contact, string billingPostalCode, string billingDepartmentJobTitle, string photoFileName, DateTime? photoDate);
    }
    public class MemberRepository : IMemberRepository
    {
        public Member GetMemberByID(string memberId)
        {
            Member mem = new Member();
            MemberDataAccess memberDB = new MemberDataAccess();
            DataSet ds = memberDB.GetMemberById(memberId);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                mem = ConvertToMember(dt);
            }
            return mem;
        }


        public Member ConvertToMember(DataTable dt)
        {
            Member mem = new Member();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    // for [T個人情報] table
                    mem.AccountNumber = row["会員番号"] != null ? row["会員番号"].ToString() : string.Empty;
                    mem.KanaName = row["カナ名"] != null ? row["カナ名"].ToString() : string.Empty;
                    mem.Name = row["名称"] != null ? row["名称"].ToString() : string.Empty;
                    mem.GenderClassification = row["性別区分"] != null ? row["性別区分"].ToString() : string.Empty;
                    mem.DateOfBirthEraClassification = row["生年月日元号区分"] != null ? row["生年月日元号区分"].ToString() : string.Empty;
                    mem.DateOfBirth = row["生年月日"] != null ? Helper.ToNullableDateTime(row["生年月日"].ToString()) : (DateTime?)null;
                    mem.Date = row["登録日"] != null ? Helper.ToNullableDateTime(row["登録日"].ToString()) : (DateTime?)null;
                    mem.UseCourse = row["口座名義"] != null ? row["口座名義"].ToString() : string.Empty;
                    mem.Level = row["口座名義"] != null ? row["口座名義"].ToString() : string.Empty;
                    mem.BodyTypeClassification = row["口座名義"] != null ? row["口座名義"].ToString() : string.Empty;
                    mem.Zip = row["郵便番号"] != null ? row["郵便番号"].ToString() : string.Empty;
                    mem.Address1 = row["住所１"] != null ? row["住所１"].ToString() : string.Empty;
                    mem.Address2 = row["住所２"] != null ? row["住所２"].ToString() : string.Empty;
                    mem.PhoneNumber = row["電話番号"] != null ? row["電話番号"].ToString() : string.Empty;
                    mem.Gender = row["住所１"] != null ? row["住所１"].ToString() : string.Empty;
                    mem.DateOfBirthYear = row["生年月日年"] != null ? Helper.ToNullableInt32(row["生年月日年"].ToString()) : (int?)null;
                    mem.DateOfBirthMonth = row["生年月日月"] != null ? Helper.ToNullableInt32(row["生年月日月"].ToString()) : (int?)null;
                    mem.DateOfBirthDay = row["生年月日日"] != null ? Helper.ToNullableInt32(row["生年月日日"].ToString()) : (int?)null;
                    mem.Kana = row["カナ"] != null ? row["カナ"].ToString() : string.Empty;
                    mem.AreaCode = row["地区コード"] != null ? row["地区コード"].ToString() : string.Empty;
                    //mem.DateOfBirthJapaneseCalendar = row["生年月日和暦"] != null ? Helper.ToNullableInt32(row["生年月日和暦"].ToString()) : (int?)null;
                    mem.UseCourse = row["住所１"] != null ? row["住所１"].ToString() : string.Empty;

                    // for [K会員口座]] table
                    mem.BillingCode = row["請求先コード"] != null ? row["請求先コード"].ToString() : string.Empty;
                    mem.BillingClassification = row["請求区分"] != null ? row["請求区分"].ToString() : string.Empty;
                    mem.PaymentMethod = row["支払方法"] != null ? row["支払方法"].ToString() : string.Empty;
                    mem.PaymentDivision = row["支払区分"] != null ? row["支払区分"].ToString() : string.Empty;
                    mem.YearBillingMonth = row["支払区分"] != null ? Helper.ToNullableInt32(row["支払区分"].ToString()) : (int?)null;
                    mem.NumberOfUnits = row["口数"] != null ? Helper.ToNullableInt32(row["口数"].ToString()) : (int?)null;
                    mem.BankCode = row["銀行コード"] != null ? Helper.ToNullableInt32(row["銀行コード"].ToString()) : (int?)null;
                    mem.BranchCode = row["支払区分"] != null ? Helper.ToNullableInt32(row["支払区分"].ToString()) : (int?)null;
                    mem.DepositEvent = row["預金種目"] != null ? row["預金種目"].ToString() : string.Empty;
                    mem.Account = row["預金口座"] != null ? row["預金口座"].ToString() : string.Empty;
                    mem.SubscriberNumber = row["支払区分"] != null ? row["支払区分"].ToString() : string.Empty;
                    mem.AccountName = row["請求先コード"] != null ? row["請求先コード"].ToString() : string.Empty;
                    mem.PayeeBankAccountCode = row["請求先コード"] != null ? row["請求先コード"].ToString() : string.Empty;
                    mem.ParentMemberShipNumber = row["親会員番号"] != null ? row["親会員番号"].ToString() : string.Empty;
                    mem.BankBranchCode = row["請求先コード"] != null ? row["請求先コード"].ToString() : string.Empty;
                    mem.PreliminaryFlag1 = row["予備フラグ1"] != null ? row["予備フラグ1"].ToString() : string.Empty;
                    mem.PreliminaryFlag2 = row["予備フラグ2"] != null ? row["予備フラグ2"].ToString() : string.Empty;
                    mem.Reserve2 = row["予備1"] != null ? row["予備1"].ToString() : string.Empty;
                    mem.Preliminary = row["予備"] != null ? Helper.ToNullableInt32(row["予備"].ToString()) : (int?)null;


                    // for [K会員情報] table
                    mem.WorkPostalCode = row["勤務先郵便番号"] != null ? row["勤務先郵便番号"].ToString() : string.Empty;
                    mem.WorkAddress1 = row["勤務先住所１"] != null ? row["勤務先住所１"].ToString() : string.Empty;
                    mem.WorkAddress2 = row["勤務先住所２"] != null ? row["勤務先住所２"].ToString() : string.Empty;
                    mem.WorkName = row["勤務先名称"] != null ? row["勤務先名称"].ToString() : string.Empty;
                    mem.WorkDepartmentJobTitle = row["勤務先部課役職名"] != null ? row["勤務先部課役職名"].ToString() : string.Empty;
                    mem.EmployerResponsibleName = row["勤務先責任者名"] != null ? row["勤務先責任者名"].ToString() : string.Empty;
                    mem.WorkPhoneNumber = row["勤務先電話番号"] != null ? row["勤務先電話番号"].ToString() : string.Empty;
                    mem.MemberType = row["会員種別"] != null ? row["会員種別"].ToString() : string.Empty;
                    mem.MemberClassification = row["会員区分"] != null ? row["会員区分"].ToString() : string.Empty;
                    mem.UseStartYear = row["利用開始年"] != null ? Helper.ToNullableInt32(row["利用開始年"].ToString()) : (int?)null;
                    mem.UseStartMonth = row["利用開始月"] != null ? Helper.ToNullableInt32(row["利用開始月"].ToString()) : (int?)null;
                    mem.UseStartDate = row["利用開始月"] != null ? Helper.ToNullableDateTime(row["利用開始月"].ToString()) : (DateTime?)null;
                    mem.DMDestination = row["DM送付先"] != null ? Helper.ToNullableInt32(row["DM送付先"].ToString()) : (int?)null;
                    mem.TotalNumberOfTimesOfUse = row["総利用回数"] != null ? Helper.ToNullableInt32(row["総利用回数"].ToString()) : (int?)null;
                    mem.EndUseDate = row["最終利用日"] != null ? Helper.ToNullableDateTime(row["最終利用日"].ToString()) : (DateTime?)null;
                    mem.LastCheckIn = row["最終チェックイン時"] != null ? Helper.ToNullableInt32(row["最終チェックイン時"].ToString()) : (int?)null;
                    mem.LastMinuteCheckIn = row["最終チェックイン分"] != null ? Helper.ToNullableInt32(row["最終チェックイン分"].ToString()) : (int?)null;
                    mem.JoinDate = row["入会日"] != null ? Helper.ToNullableDateTime(row["入会日"].ToString()) : (DateTime?)null;
                    mem.WithdrawalDate = row["退会日"] != null ? Helper.ToNullableDateTime(row["退会日"].ToString()) : (DateTime?)null;
                    mem.ChangeProcessDate = row["変更処理日"] != null ? Helper.ToNullableDateTime(row["変更処理日"].ToString()) : (DateTime?)null;
                    mem.ReasonForStoppingUse = row["使用停止理由"] != null ? row["使用停止理由"].ToString() : string.Empty;
                    mem.Hospital = row["病院"] != null ? row["病院"].ToString() : string.Empty;
                    mem.Contact = row["連絡先"] != null ? row["連絡先"].ToString() : string.Empty;
                    mem.BillingPostalCode = row["請求先郵便番号"] != null ? row["請求先郵便番号"].ToString() : string.Empty;
                    mem.BillingAddress1 = row["請求先住所１"] != null ? row["請求先住所１"].ToString() : string.Empty;
                    mem.BillingAddress2 = row["請求先住所２"] != null ? row["請求先住所２"].ToString() : string.Empty;
                    mem.TheBillToName = row["請求先名称"] != null ? row["請求先名称"].ToString() : string.Empty;
                    mem.BillingDepartmentJobTitle = row["請求先部課役職名"] != null ? row["請求先部課役職名"].ToString() : string.Empty;
                    mem.BillingResponsibleName = row["請求先責任者名"] != null ? row["請求先責任者名"].ToString() : string.Empty;
                    mem.BillingPhoneNumber = row["請求先電話番号"] != null ? row["請求先電話番号"].ToString() : string.Empty;
                    mem.MemberNext = row["会員次"] != null ? row["会員次"].ToString() : string.Empty;
                    mem.Note1 = row["備考１"] != null ? row["備考１"].ToString() : string.Empty;
                    mem.Note2 = row["備考２"] != null ? row["備考２"].ToString() : string.Empty;
                    mem.Note3 = row["備考３"] != null ? row["備考３"].ToString() : string.Empty;
                    mem.Note4 = row["備考４"] != null ? row["備考４"].ToString() : string.Empty;
                    mem.PhotoFileName = row["写真ファイル名"] != null ? row["写真ファイル名"].ToString() : string.Empty;
                    mem.PhotoDate = row["写真更新日"] != null ? Helper.ToNullableDateTime(row["写真更新日"].ToString()) : (DateTime?)null;

                }
            }

            return mem;
        }


        #region IMemberRepository Members


        public int SaveMember(RequestType type, string accountNumber, string kanaName, string name, string genderClassification, DateTime? dateOfBirth, string zip, string address1, string address2, string phoneNumber
            , int? DateOfBirthYear, int? dateOfBirthMonth, int? dateOfBirthDay
            , string areaCode, string billingCode, string billingClassification
            , string paymentMethod, string paymentDivision, int? yearBillingMonth, int? numberOfUnits, int? bankCode
            , int? branchCode, string depositEvent, string account, string accountName, string parentMembershipNumber
            , string workPostalCode, string workAddress2, string workPhoneNumber, string memberClassification
            , int? dMDestination, DateTime? withdrawalDate, DateTime? changeProcessDate, string theReasonForStoppingUse
            , string hospital, string contact, string billingPostalCode, string billingDepartmentJobTitle, string photoFileName, DateTime? photoDate)
        {
            Member mem = new Member();
            MemberDataAccess memberDB = new MemberDataAccess();
            string procName = string.Empty;
            if (type.GetHashCode().ToString().Equals(RequestType.Add))
            {
                procName = "InsertMember";
            }
            else
            {
                procName = "UpdateMember";
            }
            return memberDB.SaveMemberToDB(procName, accountNumber, kanaName, name, genderClassification, dateOfBirth, zip, address1, address2, phoneNumber
            , DateOfBirthYear, dateOfBirthMonth, dateOfBirthDay
            , areaCode, billingCode, billingClassification
            , paymentMethod, paymentDivision, yearBillingMonth, numberOfUnits, bankCode
            , branchCode, depositEvent, account, accountName, parentMembershipNumber
            , workPostalCode, workAddress2, workPhoneNumber, memberClassification
            , dMDestination, withdrawalDate, changeProcessDate, theReasonForStoppingUse
            , hospital, contact, billingPostalCode, billingDepartmentJobTitle, photoFileName, photoDate);
        }

        public PostalCode GetPostalCodeByZipCode(string zipCode)
        {
            //PostalCode postal = new PostalCode();
            PostalCodeAccess postDB = new PostalCodeAccess();
            DataSet ds = postDB.GetPostalCodeByZipCode(zipCode);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return ConvertToPostalCode(dt);
            }
            return null;
        }

        public PostalCode ConvertToPostalCode(DataTable dt)
        {
            PostalCode post = new PostalCode();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    post.City = row["市区町村"] != null ? row["市区町村"].ToString() : string.Empty;
                    post.State = row["都道府県"] != null ? row["都道府県"].ToString() : string.Empty;
                    post.TownArea = row["町域"] != null ? row["町域"].ToString() : string.Empty;
                }
            }
            return post;
        }

        public District GetDistrictByStateCode(string stateCode)
        {
            //PostalCode postal = new PostalCode();
            DistrictAccess disDB = new DistrictAccess();
            DataSet ds = disDB.GetDistrictByStateCode(stateCode);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return ConvertToDistrict(dt);
            }
            return null;
        }

        public District ConvertToDistrict(DataTable dt)
        {
            District dis = new District();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    //dis.StateCode = row["地区コード"] != null ? row["地区コード"].ToString() : string.Empty;
                    dis.DistrictName = row["地区名"] != null ? row["地区名"].ToString() : string.Empty;

                }
            }
            return dis;
        }

        public Bank GetBankByBankCode(int bankCode)
        {
            //PostalCode postal = new PostalCode();
            BankAccess bankDB = new BankAccess();
            DataSet ds = bankDB.GetBankByBankCode(bankCode);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return ConvertToBank(dt);
            }
            return null;
        }

        public Bank ConvertToBank(DataTable dt)
        {
            Bank bank = new Bank();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    //dis.StateCode = row["地区コード"] != null ? row["地区コード"].ToString() : string.Empty;
                    bank.BankName = row["銀行名"] != null ? row["銀行名"].ToString() : string.Empty;

                }
            }
            return bank;
        }

        public Branch GetBranchByBranchCode(int branchCode)
        {
            //PostalCode postal = new PostalCode();
            BranchAccess bankDB = new BranchAccess();
            DataSet ds = bankDB.GetBranchByBranchCode(branchCode);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return ConvertToBranch(dt);
            }
            return null;
        }

        public Branch ConvertToBranch(DataTable dt)
        {
            Branch branch = new Branch();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    //dis.StateCode = row["地区コード"] != null ? row["地区コード"].ToString() : string.Empty;
                    branch.BranchName = row["銀行支店名"] != null ? row["銀行支店名"].ToString() : string.Empty;

                }
            }
            return branch;
        }


        #endregion

    }


}
